package vn.allmobile.newsreader.activities.main;

import java.util.List;

import vn.allmobile.newsreader.R;
import vn.allmobile.newsreader.customview.GridLayout;
import vn.allmobile.newsreader.customview.PaperCellLabel;
import vn.allmobile.newsreader.data.Paper;
import vn.allmobile.newsreader.utils.Contants;
import vn.allmobile.newsreader.utils.ImageLoader;
import vn.allmobile.newsreader.utils.Log;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainFlipViewAdapter extends BaseAdapter {
	List<Paper> _mPapers;
	public MainFlipViewAdapter(List<Paper> mPapers) {
		// TODO Auto-generated constructor stub
		_mPapers = mPapers;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		Log.v("flipview adapter:" + (int) Math.ceil((float)_mPapers.size()/Contants.NUM_ITEM_GRIDVIEW ));
		return (int) Math.ceil((float)_mPapers.size()/Contants.NUM_ITEM_GRIDVIEW );
		
		//return _mPapers.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final Context context = parent.getContext();
		Paper pp;
		//ExpandableHeightGridView v = (ExpandableHeightGridView)convertView;
		
		//ExpandableGridLayout v = (ExpandableGridLayout)convertView;
		RelativeLayout grid_layout = (RelativeLayout)convertView;
		//	GridLayout v = (GridLayout)convertView;
		
		 if(grid_layout == null)
		 {
			 LayoutInflater li = LayoutInflater.from(context);
			 grid_layout = (RelativeLayout) li.inflate(R.layout.expandable_grid,null);
		 }
		
		 //v = (ExpandableHeightGridView) grid_layout.findViewById(R.id.expandable_grid);
		 GridLayout v = (GridLayout) grid_layout.findViewById(R.id.expandable_grid);
		 
		// TextView page = (TextView)grid_layout.findViewById(R.id.grid_item_page);
		// page.setText("Page " +(position + 1)+ "of "+ getCount());
		 
		 int itemIndex;
		 for(int i=0; i<Contants.NUM_ITEM_GRIDVIEW/2; i++) {
				for(int j = 0; j<2;j ++)
				{				
					itemIndex = (2*i+j)+ position*(Contants.NUM_ITEM_GRIDVIEW -1 );							
					//Object key = keys.get(itemIndex);				
					//Log.v("paper index:" + ((2*i+j)+ position*(Variables.ITEM_PER_PAGE -1 )));
					pp = _mPapers.get(itemIndex);
					//cat = _category.get(key);
					
					if(pp != null)
					{					
					   // v.addView(item_view);
					    LayoutInflater li_item = LayoutInflater.from(context);
						View mView = li_item.inflate(R.layout.paper_item, null);
						PaperCellLabel mTitle = (PaperCellLabel) mView.findViewById(R.id.grid_item_text);
						mTitle.setText(pp.getTitle());
						//Log.v("title:" + pp.getTitle());
						ImageView mImage = (ImageView) mView.findViewById(R.id.grid_item_image);
						ImageLoader mLoaderImage = new ImageLoader(context);
						mLoaderImage.DisplayImage("http://2.stc.laban.vn/largeicons/kilobooks.com-1364873238.png", mImage);
						v.addView(mView);
						Log.v("add to view");
					}
					
				}
			    
			    // and then adding table row in tablelayout
			}
		 
		 
		// ExpandableGridViewAdapter adapter  = new ExpandableGridViewAdapter(_mPapers,position);			
		 //v.setAdapter(adapter);
		
		return grid_layout;
	}

}
